﻿<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#696969; '>' Define the HTML to PDF converter object as a class member to make it accessible in the headerHtml_NavigationCompletedEvent handler</span>
<span style='color:#696969; '>' where the header height will be automatically adjusted</span>
<span style='color:#800000; font-weight:bold; '>Private</span> htmlToPdfConverter <span style='color:#800000; font-weight:bold; '>As</span> HtmlToPdfConverter

<span style='color:#696969; '>' Indicates if a line should be drawn at the botom of the header</span>
<span style='color:#800000; font-weight:bold; '>Private</span> drawHeaderLine <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Boolean</span> <span style='color:#808030; '>=</span> <span style='color:#0f4d75; '>True</span>

<span style='color:#800000; font-weight:bold; '>Protected</span> <span style='color:#800000; font-weight:bold; '>Sub</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>ByVal</span> sender <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Object</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>ByVal</span> e <span style='color:#800000; font-weight:bold; '>As</span> EventArgs<span style='color:#808030; '>)</span>
    <span style='color:#696969; '>' Create a HTML to PDF converter object with default settings</span>
    htmlToPdfConverter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>New</span> HtmlToPdfConverter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>

    <span style='color:#696969; '>' Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>' Leave it not set to use the converter in demo mode</span>
    htmlToPdfConverter<span style='color:#008c00; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#0000e6; '>"4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="</span>

    <span style='color:#696969; '>' Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed</span>
    <span style='color:#696969; '>' Set this property to 0 if you don't need to wait for such asynchcronous operations to finish</span>
    htmlToPdfConverter<span style='color:#008c00; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#008c00; '>2</span>

    <span style='color:#696969; '>' Enable header in the generated PDF document</span>
    htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>ShowHeader <span style='color:#808030; '>=</span> <span style='color:#0f4d75; '>True</span>

    <span style='color:#800000; font-weight:bold; '>Dim</span> headerHtmlUrl <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>String</span> <span style='color:#808030; '>=</span> Server<span style='color:#008c00; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"~/DemoAppFiles/Input/HTML_Files/Header_HTML.html"</span><span style='color:#808030; '>)</span>
    <span style='color:#800000; font-weight:bold; '>Dim</span> documentObject <span style='color:#800000; font-weight:bold; '>As</span> Document <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>Nothing</span>
    <span style='color:#800000; font-weight:bold; '>Try</span>
        <span style='color:#800000; font-weight:bold; '>If</span> autoResizeHeaderRadioButton<span style='color:#008c00; '>.</span>Checked <span style='color:#800000; font-weight:bold; '>Then</span>
            <span style='color:#696969; '>' Create a HTML element to be added in header</span>
            <span style='color:#800000; font-weight:bold; '>Dim</span> headerHtml <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> HtmlToPdfElement<span style='color:#808030; '>(</span>headerHtmlUrl<span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Install a handler where to set the automatically calculated header height</span>
            <span style='color:#800000; font-weight:bold; '>AddHandler</span> headerHtml<span style='color:#008c00; '>.</span>NavigationCompletedEvent<span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>AddressOf</span> headerHtml_NavigationCompletedEvent

            <span style='color:#696969; '>' Add the HTML element to header</span>
            <span style='color:#696969; '>' When the element is rendered in header by converter, the headerHtml_NavigationCompletedEvent handler </span>
            <span style='color:#696969; '>' will be invoked and the header height will be automatically calculated</span>
            htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfHeaderOptions<span style='color:#008c00; '>.</span>AddElement<span style='color:#808030; '>(</span>headerHtml<span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Call the converter to produce a Document object</span>
            documentObject <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>ConvertUrlToPdfDocumentObject<span style='color:#808030; '>(</span>urlTextBox<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Text</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Uninstall the handler</span>
            <span style='color:#800000; font-weight:bold; '>RemoveHandler</span> headerHtml<span style='color:#008c00; '>.</span>NavigationCompletedEvent<span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>AddressOf</span> headerHtml_NavigationCompletedEvent

            <span style='color:#696969; '>' Draw a line at the header bottom</span>
            <span style='color:#800000; font-weight:bold; '>If</span> drawHeaderLine <span style='color:#800000; font-weight:bold; '>Then</span>
                <span style='color:#800000; font-weight:bold; '>Dim</span> headerWidth <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> documentObject<span style='color:#008c00; '>.</span>Header<span style='color:#008c00; '>.</span>Width
                <span style='color:#800000; font-weight:bold; '>Dim</span> headerHeight <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> documentObject<span style='color:#008c00; '>.</span>Header<span style='color:#008c00; '>.</span>Height

                <span style='color:#696969; '>' Create a line element for the bottom of the header</span>
                <span style='color:#800000; font-weight:bold; '>Dim</span> headerLine <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> LineElement<span style='color:#808030; '>(</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>,</span> headerHeight <span style='color:#808030; '>-</span> <span style='color:#008c00; '>1</span><span style='color:#808030; '>,</span> headerWidth<span style='color:#808030; '>,</span> headerHeight <span style='color:#808030; '>-</span> <span style='color:#008c00; '>1</span><span style='color:#808030; '>)</span>

                <span style='color:#696969; '>' Set line color</span>
                headerLine<span style='color:#008c00; '>.</span>ForeColor <span style='color:#808030; '>=</span> Color<span style='color:#008c00; '>.</span>Gray

                <span style='color:#696969; '>' Add line element to the bottom of the header</span>
                documentObject<span style='color:#008c00; '>.</span>Header<span style='color:#008c00; '>.</span>AddElement<span style='color:#808030; '>(</span>headerLine<span style='color:#808030; '>)</span>
            <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>

            <span style='color:#696969; '>' Save the PDF document in a memory buffer</span>
            <span style='color:#800000; font-weight:bold; '>Dim</span> outPdfBuffer<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Byte</span> <span style='color:#808030; '>=</span> documentObject<span style='color:#008c00; '>.</span>Save<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Send the PDF as response to browser</span>

            <span style='color:#696969; '>' Set response content type</span>
            Response<span style='color:#008c00; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Content-Type"</span><span style='color:#808030; '>,</span> <span style='color:#0000e6; '>"application/pdf"</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Instruct the browser to open the PDF file as an attachment or inline</span>
            Response<span style='color:#008c00; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Content-Disposition"</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>String</span><span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Format</span><span style='color:#808030; '>(</span><span style='color:#0000e6; '>"attachment; filename=Auto_Resize_Header_Footer.pdf; size={0}"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#008c00; '>.</span>Length<span style='color:#008c00; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Write the PDF document buffer to HTTP response</span>
            Response<span style='color:#008c00; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' End the HTTP response and stop the current page processing</span>
            Response<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>End</span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>
        <span style='color:#800000; font-weight:bold; '>Else</span>
            <span style='color:#696969; '>' Create a HTML to PDF element to be added in header</span>
            <span style='color:#800000; font-weight:bold; '>Dim</span> headerHtml <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> HtmlToPdfElement<span style='color:#808030; '>(</span>headerHtmlUrl<span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Set a fixed header height in points</span>
            htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfHeaderOptions<span style='color:#008c00; '>.</span>HeaderHeight <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>Single</span><span style='color:#008c00; '>.</span>Parse<span style='color:#808030; '>(</span>headerHeightTextBox<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Text</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Set the HTML element to fit the container height</span>
            headerHtml<span style='color:#008c00; '>.</span>FitHeight <span style='color:#808030; '>=</span> <span style='color:#0f4d75; '>True</span>

            <span style='color:#696969; '>' Add HTML element to fit the fixed header height</span>
            htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfHeaderOptions<span style='color:#008c00; '>.</span>AddElement<span style='color:#808030; '>(</span>headerHtml<span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Draw a line at the header bottom</span>
            <span style='color:#800000; font-weight:bold; '>If</span> drawHeaderLine <span style='color:#800000; font-weight:bold; '>Then</span>
                <span style='color:#696969; '>' Calculate the header width based on PDF page size and margins</span>
                <span style='color:#800000; font-weight:bold; '>Dim</span> headerWidth <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>PdfPageSize<span style='color:#008c00; '>.</span>Width <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>LeftMargin <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>RightMargin

                <span style='color:#696969; '>' Calculate header height</span>
                <span style='color:#800000; font-weight:bold; '>Dim</span> headerHeight <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfHeaderOptions<span style='color:#008c00; '>.</span>HeaderHeight

                <span style='color:#696969; '>' Create a line element for the bottom of the header</span>
                <span style='color:#800000; font-weight:bold; '>Dim</span> headerLine <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>New</span> LineElement<span style='color:#808030; '>(</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>,</span> headerHeight <span style='color:#808030; '>-</span> <span style='color:#008c00; '>1</span><span style='color:#808030; '>,</span> headerWidth<span style='color:#808030; '>,</span> headerHeight <span style='color:#808030; '>-</span> <span style='color:#008c00; '>1</span><span style='color:#808030; '>)</span>

                <span style='color:#696969; '>' Set line color</span>
                headerLine<span style='color:#008c00; '>.</span>ForeColor <span style='color:#808030; '>=</span> Color<span style='color:#008c00; '>.</span>Gray

                <span style='color:#696969; '>' Add line element to the bottom of the header</span>
                htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfHeaderOptions<span style='color:#008c00; '>.</span>AddElement<span style='color:#808030; '>(</span>headerLine<span style='color:#808030; '>)</span>
            <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>

            <span style='color:#696969; '>' Convert the HTML page to a PDF document in a memory buffer</span>
            <span style='color:#800000; font-weight:bold; '>Dim</span> outPdfBuffer<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Byte</span> <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>ConvertUrl<span style='color:#808030; '>(</span>urlTextBox<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Text</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Send the PDF as response to browser</span>

            <span style='color:#696969; '>' Set response content type</span>
            Response<span style='color:#008c00; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Content-Type"</span><span style='color:#808030; '>,</span> <span style='color:#0000e6; '>"application/pdf"</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Instruct the browser to open the PDF file as an attachment or inline</span>
            Response<span style='color:#008c00; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"Content-Disposition"</span><span style='color:#808030; '>,</span> <span style='color:#800000; font-weight:bold; '>String</span><span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Format</span><span style='color:#808030; '>(</span><span style='color:#0000e6; '>"attachment; filename=Auto_Resize_Header_Footer.pdf; size={0}"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#008c00; '>.</span>Length<span style='color:#008c00; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' Write the PDF document buffer to HTTP response</span>
            Response<span style='color:#008c00; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span>

            <span style='color:#696969; '>' End the HTTP response and stop the current page processing</span>
            Response<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>End</span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>
        <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>
    <span style='color:#800000; font-weight:bold; '>Finally</span>
        <span style='color:#696969; '>' Close the PDF document</span>
        <span style='color:#800000; font-weight:bold; '>If</span> documentObject IsNot <span style='color:#800000; font-weight:bold; '>Nothing</span> <span style='color:#800000; font-weight:bold; '>Then</span>
            documentObject<span style='color:#008c00; '>.</span><span style='color:#800000; font-weight:bold; '>Close</span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>
        <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>
    <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>Try</span>
<span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>Sub</span>

<span style='color:#696969; '>''' &lt;summary></span>
<span style='color:#696969; '>''' This handler is called after the navigation to header HTML completed. The document header is resized in this event handler</span>
<span style='color:#696969; '>''' &lt;/summary></span>
<span style='color:#696969; '>''' &lt;param name="eventParams">The event parameter containing the HTML content size in pixels and points&lt;/param></span>
<span style='color:#800000; font-weight:bold; '>Private</span> <span style='color:#800000; font-weight:bold; '>Sub</span> headerHtml_NavigationCompletedEvent<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>ByVal</span> eventParams <span style='color:#800000; font-weight:bold; '>As</span> NavigationCompletedParams<span style='color:#808030; '>)</span>
    <span style='color:#696969; '>' Get the header HTML width and height from event parameters</span>
    <span style='color:#800000; font-weight:bold; '>Dim</span> headerHtmlWidth <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> eventParams<span style='color:#008c00; '>.</span>HtmlContentWidthPt
    <span style='color:#800000; font-weight:bold; '>Dim</span> headerHtmlHeight <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> eventParams<span style='color:#008c00; '>.</span>HtmlContentHeightPt

    <span style='color:#696969; '>' Calculate the header width from coverter settings</span>
    <span style='color:#800000; font-weight:bold; '>Dim</span> headerWidth <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>PdfPageSize<span style='color:#008c00; '>.</span>Width <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>LeftMargin <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>RightMargin

    <span style='color:#696969; '>' Calculate a resize factor to fit the header width</span>
    <span style='color:#800000; font-weight:bold; '>Dim</span> resizeFactor <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> <span style='color:#008c00; '>1</span>
    <span style='color:#800000; font-weight:bold; '>If</span> headerHtmlWidth <span style='color:#808030; '>></span> headerWidth <span style='color:#800000; font-weight:bold; '>Then</span>
        resizeFactor <span style='color:#808030; '>=</span> headerWidth <span style='color:#808030; '>/</span> headerHtmlWidth
    <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>

    <span style='color:#696969; '>' Calculate the header height to preserve the HTML aspect ratio</span>
    <span style='color:#800000; font-weight:bold; '>Dim</span> headerHeight <span style='color:#800000; font-weight:bold; '>As</span> <span style='color:#800000; font-weight:bold; '>Single</span> <span style='color:#808030; '>=</span> headerHtmlHeight <span style='color:#808030; '>*</span> resizeFactor

    <span style='color:#800000; font-weight:bold; '>If</span> <span style='color:#800000; font-weight:bold; '>Not</span> <span style='color:#808030; '>(</span>headerHeight <span style='color:#808030; '>&lt;</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>PdfPageSize<span style='color:#008c00; '>.</span>Height <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>TopMargin <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>BottomMargin<span style='color:#808030; '>)</span> <span style='color:#800000; font-weight:bold; '>Then</span>
        <span style='color:#800000; font-weight:bold; '>Throw</span> <span style='color:#800000; font-weight:bold; '>New</span> Exception<span style='color:#808030; '>(</span><span style='color:#0000e6; '>"The header height cannot be bigger than PDF page height"</span><span style='color:#808030; '>)</span>
    <span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>If</span>

    <span style='color:#696969; '>' Set the calculated header height</span>
    htmlToPdfConverter<span style='color:#008c00; '>.</span>PdfDocumentOptions<span style='color:#008c00; '>.</span>DocumentObject<span style='color:#008c00; '>.</span>Header<span style='color:#008c00; '>.</span>Height <span style='color:#808030; '>=</span> headerHeight
<span style='color:#800000; font-weight:bold; '>End</span> <span style='color:#800000; font-weight:bold; '>Sub</span>
</pre>
</span>